<?php

declare(strict_types=1);

namespace app\validate\region;

use app\model\RegionUser;
use think\Validate;

class RegionUserValidate extends Validate
{
    protected $rule = [
        'id' => 'integer|gt:0',
        'region_id' => 'require|integer|gt:0',
        'user_id' => 'require|integer|gt:0',
        'role_type' => 'require|in:' . RegionUser::ROLE_MANAGER . ',' . RegionUser::ROLE_OPERATOR,
        'is_primary' => 'in:0,1',
        'status' => 'in:0,1',
        'config' => 'checkConfig',
    ];

    protected $message = [
        'region_id.require' => '区域ID不能为空',
        'region_id.integer' => '区域ID格式不正确',
        'region_id.gt' => '区域ID格式不正确',
        'user_id.require' => '用户ID不能为空',
        'user_id.integer' => '用户ID格式不正确',
        'user_id.gt' => '用户ID格式不正确',
        'role_type.require' => '区域角色不能为空',
        'role_type.in' => '区域角色不正确',
        'is_primary.in' => '主区域标记不正确',
        'status.in' => '状态不正确',
    ];

    protected $scene = [
        'create' => ['region_id', 'user_id', 'role_type', 'is_primary', 'status', 'config'],
        'update' => ['id', 'region_id', 'user_id', 'role_type', 'is_primary', 'status', 'config'],
    ];

    protected function checkConfig($value)
    {
        if ($value === null || $value === '') {
            return true;
        }

        if (!is_array($value)) {
            return '扩展配置格式不正确';
        }

        return true;
    }
}
